package com.example.springback.controller;

import com.example.springback.common.Result;
import com.example.springback.common.WordToHtmlConverter;
import com.example.springback.domain.Employee;
import com.example.springback.domain.Notice;
import com.example.springback.exception.CustomException;
import com.example.springback.service.NoticeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.docx4j.Docx4J;
import org.docx4j.convert.out.html.AbstractHtmlExporter;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    NoticeService noticeService;


    //查找所有公告
    @GetMapping("/searchPage")
    public Result selectPage(Notice notice , @RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<Notice> noticePageInfo =noticeService.selectPage(notice,pageNumber,pageSize);
        return Result.success(noticePageInfo);
    }
    @GetMapping("/searchTen")//获取前十个公告展示在首页
    public Result selectTen(Notice notice){
        List<Notice> noticePageInfo =noticeService.selectTen(notice);
        return Result.success(noticePageInfo);
    }
    @GetMapping("/{id}")
    public Result getNoticeById(@PathVariable Integer id) {
        Notice notice = noticeService.selectById(id);
        if (notice == null) {
            return Result.error("404", "公告不存在");
        }
        return Result.success(notice);
    }

    //更新公告
    @PostMapping ("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.update(notice);
        return Result.success();
    }
    //删除公告
    @PostMapping ("/delete")
    public Result delete(@RequestBody Notice notice){
        noticeService.delete(notice.getId());
        return Result.success();
    }
    //新增公告
    @PostMapping("/upload")
    public Result uploadNotice(@RequestParam("file")MultipartFile file,@ModelAttribute Notice notice) {
        // 1. 读取 docx 文件
        WordprocessingMLPackage wordMLPackage = null;
        try {
            wordMLPackage = WordprocessingMLPackage.load(file.getInputStream());
        } catch (Docx4JException e) {
            throw new CustomException("500","读取文件失败");
        } catch (IOException e) {
            throw new CustomException("500","读取文件失败");
        }
        // 2. 图片保存路径
        String imageDirPath = "src/main/resources/static/images/";
        File imageDir = new File(imageDirPath);
        if (!imageDir.exists()) imageDir.mkdirs();
        // 3. 生成 HTML
        AbstractHtmlExporter.HtmlSettings htmlSettings = new AbstractHtmlExporter.HtmlSettings();
        htmlSettings.setWmlPackage(wordMLPackage);
        htmlSettings.setImageDirPath(imageDirPath);
        htmlSettings.setImageTargetUri("/images/");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Docx4J.toHTML(htmlSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL);
        } catch (Docx4JException e) {
            throw new CustomException("500","生成HTML失败");
        }
        String htmlContent = null;
        try {
            htmlContent = out.toString(StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            throw new CustomException("500","转换失败");
        }
        // 4. 构建公告对象并入库
        notice.setContentHtml(htmlContent);
        noticeService.insert(notice);
        return Result.success();
    }
    @PostMapping("/deletebatch")
    public Result deleteBatch(@RequestBody List<Notice> notices){
        noticeService.deleteBatch(notices);
        return Result.success();
    }
}
